<?php

class ErrorViewsController extends AppController
{
	public $name = 'ErrorViews';
	
	public $uses=array('Error');
	public $layout='null';
	
	//======================================function for backend===========================================
	function admin_index(){
		//get filter field
		$filterField=array(				
			$this->uses[0].'.host'=>_resource('host',true),
			$this->uses[0].'.type'=>_resource('type',true),
			$this->uses[0].'.code'=>_resource('code',true),
			$this->uses[0].'.message'=>_resource('message',true),
			$this->uses[0].'.method'=>_resource('method',true),
			$this->uses[0].'.referer'=>_resource('referer',true),
			$this->uses[0].'.here'=>_resource('here',true),
			$this->uses[0].'.clientIp'=>_resource('clientIp',true),
			$this->uses[0].'.HTTP_USER_AGENT'=>_resource('HTTP_USER_AGENT',true),
			$this->uses[0].'.HTTP_ACCEPT'=>_resource('HTTP_ACCEPT',true),
			$this->uses[0].'.HTTP_COOKIE'=>_resource('HTTP_COOKIE',true),
			$this->uses[0].'.CONTENT_TYPE'=>_resource('CONTENT_TYPE',true),
			
		);
		
		$this->set('filterField',$filterField);
		$this->render('admin_index','ajax');
	}	
	
	function admin_lists(){
		//khoi tao cac session can thiet cho 1 model, session nay dung de luu lai trang thai cua 1 model nhu phan trang, sort tang giam, dieu kien search, id cha...
		//cac su dung Session cua cakephp : doc them trong book.cakephp.org
		if(!$this->Session->check($this->uses[0])){
			//khoi tao array vao bien $tmp
			$tmp=array(
				'limit'=>$this->webConfig['default_row_per_page'],
				'conditions'=>'',
				'fields'=>null,
				'order'=>$this->uses[0].'.id desc',
				'page'=>1,
				'recursive'=>0,
				'sortField'=>null,
				'sortDir'=>'asc',				
				'filterField'=>'',
				'filterText'=>'',
				'catid'=>'',
			);
			//luu vao session
			$this->Session->write($this->uses[0],$tmp);			
		}
		
		
		
		//doc ra cac trang thai cua 1 model da luu trong session
		// truyen vao bien paginate, khi lay data, model se dua vao cac gia tri cua bien nay de search, sort, phan trang ... 
		$this->paginate=$this->Session->read($this->uses[0]);				
		//ham paginate() la ham ho tro phan trang, khi dung ham nay, se tu dong phan trang theo cac gia tri trong bien paginate o tren
		//lay data da duoc phan trang va truyen vao bien $data0
		$data0=$this->paginate($this->uses[0]);
		
		//set bien $data0 ra view de hien ra luoi.
		$this->set('data0',$data0);
		
		
		//render view va cho cakephp biet day la view cua ajax -> khi do, cakephp se chi render view ma khong render toan bo layout cua website. 
		$this->render('admin_lists','ajax');
	}
	
	
	function admin_delete($id = null) {		
		$this->Session->delete('error');
		//if is submit:		
		if($this->request->is('post')){
			$id_arr=$this->data['check'];
		} else {
			$id_arr=null;
		}
		//if url
		if(intval($id)){
			$id_arr[$id]='';
		}
		if(count($id_arr)>0){
			//Xoa nhieu record
			$this->{$this->uses[0]}->query('delete from '.$this->{$this->uses[0]}->tablePrefix.$this->{$this->uses[0]}->table.' where id in ('.implode(',',array_keys($id_arr)).')' );			
		} else {
			$this->Session->write('error',_resource('no information to delete',true));
		}
		$this->admin_lists();		
	}
	
	
	
	function admin_sort($sortField=null,$sortDir='asc'){
		if($sortField){
			if($sortDir!='desc') $sortDir='asc';
			$this->Session->write($this->uses[0].'.sortField',$sortField);
			$this->Session->write($this->uses[0].'.sortDir',$sortDir);
			$this->Session->write($this->uses[0].'.order',$sortField.' '.$sortDir);
		}
		$this->admin_lists();
	}
	
	function admin_page($page=1){
		$this->Session->write($this->uses[0].'.page',$page);
		$this->admin_lists();
	}
	
	function admin_display($limit=0){
		if(intval($limit)){
			$this->Session->write($this->uses[0].'.limit',$limit);
		}
		$this->admin_lists();
	}

	function admin_filter(){
		if($this->request->is('post')){
			$this->Session->write($this->uses[0].'.filterText',$this->request->data['filter_text']);
			$this->Session->write($this->uses[0].'.filterField',$this->request->data['filter_field']);
			//$conditions=$this->Session->read($this->uses[0].'.conditions');
			if(!empty($this->request->data['filter_text'])){							
				$conditions[$this->request->data['filter_field'].' like']='%'.$this->request->data['filter_text'].'%';
				//t($conditions);				
			}
			else{
				unset($conditions[$this->request->data['filter_field'].' like']);
			}
			$this->Session->write($this->uses[0].'.conditions',$conditions);
		}
		$this->admin_lists();
	}
	
	/*
	 * ham dung de save thu tu cua tung mau tin
	 */
	function admin_save(){
		if(isset($this->params['form']['order'])){			
			foreach($this->params['form']['order'] as $k=>$v){
				if(intval($v)){
					//set view order for category
					$this->{$this->uses[0]}->updateAll(array($this->uses[0].'.view_order'=>intval($v)),array($this->uses[0].'.id'=>$k));
				}
			}
		}
		$this->Session->write('message',_resource('view order has been update',true));
		$this->admin_lists();
	}
	
/*
	 * ham dung khi co su dung chuc nang da ngon ngu
	 * dung de change locale cua he thong
	 * tham so truyen vao la locale cua ngon ngu
	 */
	function admin_changeLocale($locale=null){
		//luu session
		if($locale){
			$this->Session->write('adminLang',$locale);
		}
		//reload lai luoi de apply locale vua change
		$this->admin_lists();
	}
		
		
}
?>
